package de.gwtprime.client.presenter;

import com.google.web.bindery.requestfactory.shared.Receiver;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.PrimeRequestFactory;
import de.gwtprime.client.event.AnswerCompleteVoted;
import de.gwtprime.client.event.VotesAnswerCompleteLoadedEvent;
import de.gwtprime.client.event.VotesAnswerLoadedEvent;
import de.gwtprime.client.event.handler.AnswerCompleteVotedHandler;
import de.gwtprime.client.event.handler.VotesAnswerCompleteLoadedEventHandler;
import de.gwtprime.client.event.handler.VotesAnswersLoadedEventHandler;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.widgets.ArticleSectionControlWidgetInterface;
import de.gwtprime.shared.VoteType;
import de.gwtprime.shared.requestFactory.AnswerProxy;
import de.gwtprime.shared.requestFactory.QuestionProxy;
import de.gwtprime.shared.requestFactory.VoteAnswerCompletedProxy;
import de.gwtprime.shared.requestFactory.VoteAnswerProxy;
import de.gwtprime.shared.requestFactory.request.AnswerRequest;

public class VoteControlPresenterAnswerImpl extends VoteControlPresenterAbstract {

	protected AnswerProxy answer;

	public VoteControlPresenterAnswerImpl(AnswerProxy answer, QuestionProxy question,
			ClientFactory clientFactory, ArticleSectionControlWidgetInterface controlWidget){


		super(clientFactory, controlWidget, answer.getUser());

		// Only the author of the question can mark the answer as solution
		if(question!=null && clientFactory.getUser() != null && clientFactory.getUser().equals(question.getUser())
				&& !clientFactory.getUser().equals(answer.getUser()) // But question author can not vote his own answer
				){
			controlWidget.setShowCompleteVoteBtn(true);
		}else{
			controlWidget.setShowCompleteVoteBtn(false);
		}
		setVoteValue(answer.getVoteValue());
		
		this.answer = answer;
	}


	@Override
	public void voteUp() {

		super.voteUp();
		final PrimeRequestFactory requestFactory = clientFactory.getRequestFactory();
		AnswerRequest request = requestFactory.answerRequest();

		VoteAnswerProxy vote = request.create(VoteAnswerProxy.class);
		vote.setType(VoteType.UP);


		request.addVoteAndPersist(vote, clientFactory.getUser()).using(answer).fire(new Receiver<Void>() {

			@Override
			public void onSuccess(Void response) {
				AppUtils.log("Vote was persisted successfully");

			}
		});

	}

	@Override
	public void voteDown() {

		super.voteDown();

		final PrimeRequestFactory requestFactory = clientFactory.getRequestFactory();
		AnswerRequest request = requestFactory.answerRequest();

		VoteAnswerProxy vote = request.create(VoteAnswerProxy.class);
		vote.setType(VoteType.DOWN);

		request.addVoteAndPersist(vote, clientFactory.getUser()).using(answer).fire(new Receiver<Void>() {

			@Override
			public void onSuccess(Void response) {
				AppUtils.log("Vote was persisted successfully");

			}
		});

	}

	@Override
	public void initializeListeners() {
		clientFactory.getEventBus().addHandler(VotesAnswerLoadedEvent.TYPE, new VotesAnswersLoadedEventHandler() {

			private void onVotesLoaded(
					final AnswerProxy articleSection,
					VotesAnswerLoadedEvent event) {
				for(VoteAnswerProxy vote : event.getVotes()){
					if(articleSection.getId().equals(vote.getTest())){

						// Disable vote buttons if vote is already applied
						voteType = vote.getType();
						if(vote.getType()==VoteType.DOWN){
							setVoteDownBtnActiveOnUI();
						}

						else if(vote.getType()==VoteType.UP){
							setVoteUpBtnActiveOnUI();

						}
						break;
					}
				}
			}

			@Override
			public void onVotesLoaded(VotesAnswerLoadedEvent event) {
				onVotesLoaded(answer, event);
				
			}
		});


		clientFactory.getEventBus().addHandler(VotesAnswerCompleteLoadedEvent.TYPE, new VotesAnswerCompleteLoadedEventHandler() {


			@Override
			public void onVotesLoaded(VotesAnswerCompleteLoadedEvent event) {
				for(VoteAnswerCompletedProxy vote : event.getVotes()){
					if(answer.getId().equals(vote.getTest())){
						setVoteCompleteBtnActiveOnUI();
						break;
					}
				}
			}
		});
		
		clientFactory.getEventBus().addHandler(AnswerCompleteVoted.TYPE, new AnswerCompleteVotedHandler() {
			
			@Override
			public void onVoteAdded(AnswerCompleteVoted event) {
				if(!answer.equals(event.getAnswer())){
					setVoteCompleteBtnInactiveOnUI();
				}
				
			}
		});

	}


	@Override
	public void onVoteComplete() {

		super.onVoteComplete();

		final PrimeRequestFactory requestFactory = clientFactory.getRequestFactory();
		AnswerRequest request = requestFactory.answerRequest();

		VoteAnswerCompletedProxy vote = request.create(VoteAnswerCompletedProxy.class);

		request.addVoteAnswerCompleteAndPersist(vote, clientFactory.getUser()).using(answer).fire(new Receiver<Void>() {

			@Override
			public void onSuccess(Void response) {
				AppUtils.log("Vote was persisted successfully");

			}
		});
		
		clientFactory.getEventBus().fireEvent(new AnswerCompleteVoted(answer));

	}


}
